/*
 * MIT License
 *
 * Copyright (c) 2021 Evren Coşkun
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.syn.synold4s.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.evrencoskun.tableview.adapter.AbstractTableAdapter;
import com.evrencoskun.tableview.adapter.recyclerview.holder.AbstractViewHolder;
import com.syn.synold4s.R;
import com.syn.synold4s.bean.dto.GassDto;

public class TableViewAdapter extends AbstractTableAdapter<String, String, GassDto> {

    @NonNull
    @Override
    public AbstractViewHolder onCreateCellViewHolder(ViewGroup parent, int viewType) {
        // 创建单元格的视图
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.table_cell_layout, parent, false);
        return new MyCellViewHolder(view);
    }

    @Override
    public void onBindCellViewHolder(@NonNull AbstractViewHolder holder, @Nullable GassDto gassItem, int columnPosition, int rowPosition) {
        MyCellViewHolder viewHolder = (MyCellViewHolder) holder;
        if (gassItem != null) {
            viewHolder.gassItem.setValue(gassItem.getVal(), gassItem.getCode());
            viewHolder.gassItem.setTime(gassItem.getPortalAt());
        }
    }

    @NonNull
    @Override
    public AbstractViewHolder onCreateColumnHeaderViewHolder(ViewGroup parent, int viewType) {
        // 创建列标题的视图
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.table_column_header_layout, parent, false);
        return new MyColumnHeaderViewHolder(view);
    }

    @Override
    public void onBindColumnHeaderViewHolder(@NonNull AbstractViewHolder holder, @Nullable String columnHeader, int columnPosition) {
        MyColumnHeaderViewHolder viewHolder = (MyColumnHeaderViewHolder) holder;
        viewHolder.textView.setText(columnHeader);
    }

    @NonNull
    @Override
    public AbstractViewHolder onCreateRowHeaderViewHolder(ViewGroup parent, int viewType) {
        // 创建行标题的视图
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.table_row_header_layout, parent, false);
        return new MyRowHeaderViewHolder(view);
    }

    @Override
    public void onBindRowHeaderViewHolder(@NonNull AbstractViewHolder holder, @Nullable String rowHeader, int rowPosition) {
        MyRowHeaderViewHolder viewHolder = (MyRowHeaderViewHolder) holder;
        viewHolder.textView.setText(rowHeader);
    }

    @NonNull
    @Override
    public View onCreateCornerView(ViewGroup parent) {
        // 角视图（表格左上角）
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.table_corner_layout, parent, false);
    }
}
